package com.itheima.service;

import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.OrderSettingMapper;
import com.itheima.pojo.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;


    @Override
    public void add(OrderSetting orderSetting) {

        Date orderDate = orderSetting.getOrderDate();
        Integer count = orderSettingMapper.queryByDate(orderDate);
        if (count > 0) {
            orderSettingMapper.update(orderSetting.getNumber(), orderDate);
        } else {
            orderSettingMapper.add(orderSetting);
        }
    }

    @Override
    public List<OrderSetting> findPage(String date) {
        String start = date+"-01";
        String end = date+"-31";
        return orderSettingMapper.findPage(start, end);
    }

    @Override
    public void upload(List<OrderSetting> orderSettings) {
        for (OrderSetting orderSetting : orderSettings) {
            add(orderSetting);
        }
    }

    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<OrderSetting> list = orderSettingMapper.findAll(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),list);
    }

    @Override
    public List<Setmeal> findAllSetmeal() {
        return orderSettingMapper.findAllSetmeal();
    }
}
